/*
 *
 *  * Copyright 2022  腾羚智能, Inc. All rights reserved.
 *
 */

package cn.wuxing.mediation.web.controller.business;

import cn.hutool.core.collection.CollUtil;
import cn.wuxing.mediation.businessplatform.domain.dto.DelDTO;
import cn.wuxing.mediation.businessplatform.domain.dto.UploadMaterialDTO;
import cn.wuxing.mediation.businessplatform.domain.dto.UploadMaterialDetailDTO;
import cn.wuxing.mediation.businessplatform.factory.BusinessFactory;
import cn.wuxing.mediation.businessplatform.mapper.business.CmEventFileMapper;
import cn.wuxing.mediation.businessplatform.service.BaseBusinessListService;
import cn.wuxing.mediation.businessplatform.service.BaseBusinessOperateService;
import cn.wuxing.mediation.common.core.controller.BaseController;
import cn.wuxing.mediation.common.core.domain.AjaxResult;
import cn.wuxing.mediation.common.core.domain.entity.CmEventFile;
import cn.wuxing.mediation.common.core.page.TableDataInfo;
import cn.wuxing.mediation.common.exception.ServiceException;
import cn.wuxing.mediation.framework.config.ServerConfig;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 业务
 *
 * @author
 * @date 2021-12-24 10:43
 */
@Slf4j
@RestController
@RequestMapping("/business")
@SuppressWarnings("all")
public class BusinessController extends BaseController {

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private CmEventFileMapper cmEventFileMapper;

    private static final String FILE_DELIMETER = ",";

    /**
     * 业务列表
     *
     * @param request
     * @param query
     *
     * @return
     */
    @RequestMapping(value = "/{tag}/getBusinessList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "事项列表", notes = "")
    public TableDataInfo getBusinessList(@PathVariable("tag") String tag, HttpServletRequest request) {
        try {
            return getDataTable(BusinessFactory.doInstanceRealization(tag, BaseBusinessListService.class)
                    .getBusinessList(request));
        } catch (Exception e) {
            log.error("getBusinessList [] getBusinessList is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }

    @RequestMapping(value = "/{tag}/operateEvent", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "操作", notes = "")
    public AjaxResult operateEvent(@PathVariable("tag") String tag, @RequestBody String request) {
        try {
            BusinessFactory.doInstanceRealization(tag, BaseBusinessOperateService.class)
                    .operateEvent(request);
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("saveEvent [] saveEvent is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }

    @RequestMapping(value = "/{tag}/getEventDetail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "详情", notes = "")
    public AjaxResult getEventDetail(@PathVariable("tag") String tag, Long id) {
        try {
            return AjaxResult.success(
                    BusinessFactory.doInstanceRealization(tag, BaseBusinessOperateService.class).getEventDetail(id));
        } catch (Exception e) {
            log.error("saveEvent [] saveEvent is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }

    @RequestMapping(value = "/{tag}/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除", notes = "")
    public AjaxResult del(@PathVariable("tag") String tag, @RequestBody DelDTO dto) {
        try {
            return AjaxResult.success(
                    BusinessFactory.doInstanceRealization(tag, BaseBusinessOperateService.class).del(dto));
        } catch (Exception e) {
            log.error("saveEvent [] saveEvent is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }

    @RequestMapping(value = "/{tag}/importTemplate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "导出", notes = "")
    public void importTemplate(@PathVariable("tag") String tag, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            BusinessFactory.doInstanceRealization(tag, BaseBusinessListService.class).importTemplate(request, response);
        } catch (Exception e) {
            log.error("saveEvent [] saveEvent is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 业务列表
     *
     * @param request
     * @param query
     *
     * @return
     */
    @RequestMapping(value = "/{tag}/getApplicantHistoryNum", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "", notes = "")
    public AjaxResult getApplicantHistoryNum(@PathVariable("tag") String tag, String idCard) {
        try {
            return AjaxResult.success(BusinessFactory.doInstanceRealization(tag, BaseBusinessListService.class)
                    .getBusinessLgetApplicantHistoryNum(idCard));
        } catch (Exception e) {
            log.error("getBusinessList [] getBusinessList is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 业务列表
     *
     * @param request
     * @param query
     *
     * @return
     */
    @RequestMapping(value = "/{tag}/getApplicantHistoryList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "", notes = "")
    public AjaxResult getApplicantHistoryList(@PathVariable("tag") String tag, String idCard) {
        try {
            return AjaxResult.success(BusinessFactory.doInstanceRealization(tag, BaseBusinessListService.class)
                    .getApplicantHistoryList(idCard));
        } catch (Exception e) {
            log.error("getBusinessList [] getBusinessList is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 转窗口确认详情
     *
     * @param request
     * @param query
     *
     * @return
     */
    @RequestMapping(value = "/{tag}/changeWindowDetail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "转窗口确认详情", notes = "")
    public AjaxResult changeWindowDetail(@PathVariable("tag") String tag, Long eventId) {
        try {
            return AjaxResult.success(BusinessFactory.doInstanceRealization(tag, BaseBusinessListService.class)
                    .changeWindowDetail(eventId));
        } catch (Exception e) {
            log.error("getBusinessList [] getBusinessList is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 上传材料
     *
     * @param request
     * @param query
     *
     * @return
     */
    @RequestMapping(value = "/upload/material", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "上传材料", notes = "")
    public AjaxResult uploadMaterial(@RequestBody UploadMaterialDTO uploadMaterialDTO) {
        try {
            List<UploadMaterialDetailDTO> detailDTOS = uploadMaterialDTO.getList();
            if (CollUtil.isEmpty(detailDTOS)) {
                return AjaxResult.warn("上传文件为空");
            }

            List<CmEventFile> cmEventFiles = new LinkedList<>();
            for (UploadMaterialDetailDTO dto : detailDTOS) {
                if (Arrays.asList("16", "17").contains(dto.getFileType())) {
                   continue;
                }
                this.cmEventFileMapper.delete(Wrappers.<CmEventFile>lambdaQuery()
                        .eq(CmEventFile::getEventId, uploadMaterialDTO.getEventId())
                        .eq(CmEventFile::getEventCategory, dto.getEventCategory())
                        .eq(CmEventFile::getFileType, dto.getFileType()));

            }

            for (UploadMaterialDetailDTO dto : detailDTOS) {
                CmEventFile cmEventFile = new CmEventFile();
                cmEventFile.setOriginalFileName(dto.getOriginalFileName());
                cmEventFile.setEventId(uploadMaterialDTO.getEventId());
                cmEventFile.setEventCategory(dto.getEventCategory());
                cmEventFile.setFileType(dto.getFileType());
                cmEventFile.setFileUrl(dto.getUrl());
                cmEventFile.setTemplate(dto.getTemplate());
                this.cmEventFileMapper.insert(cmEventFile);
                cmEventFiles.add(cmEventFile);
            }
            return AjaxResult.success(cmEventFiles);
        } catch (Exception e) {
            log.error("getBusinessList [] getBusinessList is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 删除材料
     *
     * @param request
     * @param query
     *
     * @return
     */
    @RequestMapping(value = "/del/material", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除材料", notes = "")
    public AjaxResult delMaterial(@RequestBody CmEventFile dto) {
        if (null == dto.getId()) {
            return AjaxResult.warn("id不能为空");
        }
        LambdaUpdateWrapper<CmEventFile> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(CmEventFile::getId, dto.getId());
        updateWrapper.set(CmEventFile::getDelFlag, 2);
        this.cmEventFileMapper.update(new CmEventFile(), updateWrapper);
        return AjaxResult.success();
    }

    @RequestMapping(value = "/{tag}/checkCloseCase", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "校验结案", notes = "")
    public AjaxResult checkCloseCase(@PathVariable("tag") String tag, Long eventId) {
        try {
            BusinessFactory.doInstanceRealization(tag, BaseBusinessOperateService.class)
                    .checkCloseCase(eventId);
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("getBusinessList [] getBusinessList is error case is : ", e);
            throw new ServiceException(e.getMessage());
        }
    }
}

